<?php

/**
 * Controller_Assess
 年度考核
 */

class Controller_assess extends MyApp {

	public function __construct()
	{
		parent::__construct();

		$this->model->record 	= YOU::loadModel('record');
		$this->model->semester 	= YOU::loadModel('semester');
		$this->model->staff 	= YOU::loadModel('staff');
	}

	/** 首页 **/
	public function actionIndex()
	{

	}
	/** 年度考核列表 **/
	public function actionAssessList()
	{	
		// 有考核记录的所有学年
		$assess_lists = $this->db->fetchAll("SELECT  distinct assess_year FROM `tbl_assess` ");
		
		// 搜索
		$time   	= $this->get->time_name;
		if (empty($time)) {
			$time = date('Y',time());
		}
		$where = '';
		if (!empty($time)) {
			$where .= " AND assess_year ={$time} ";
			$this->tpl->assign("time", $time);
		}
		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_staff`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$staff_lists = $this->db->fetchAll("SELECT staff_name,staff_number,staff_id FROM `tbl_staff`  ORDER BY staff_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach ($staff_lists as $key => $value){
			// 查找历史记录
			$row = $this->db->fetchRow("SELECT * FROM `tbl_assess` WHERE staff_id ='{$value['staff_id']}' {$where}");
			$staff_lists[$key]['assess_result']= $this->assess_ini[$row['assess_result']];

		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("assess_lists", $assess_lists);
	
		$this->tpl->display("assess/assess.list.tpl.php");
	}

	/** 年度考核修改 **/
	public function actionAssessUpdate()
	{
		$staff_id   	= (int)$this->get->staff_id;
		$assess_result  = $this->assess_ini;
		// 当前年份
		$time = date('Y',time());
		// 老师名称
		$staff_data = $this->model->staff->getinfo($staff_id);
		// 年度考核是否存在

		$assess_data = $this->db->fetchRow("SELECT * FROM `tbl_assess` WHERE staff_id ='{$staff_id}' AND assess_year='{$time}'");
		if ($_POST){

			$staff_assess		  = $this->post->staff_assess;
			if (empty($assess_data)) {
				// 写入数据库
				$info = array(
				"staff_id"		  		=> $staff_id,
				"semester_id"		  	=> $semester_id,
				"assess_result"		  	=> $staff_assess,
				"assess_year"		  	=> $time,
				"status"		  		=> 1,
				"create_time"         	=> date("Y-m-d H:i:s"),
				"update_time"         	=> "0000-00-00 00:00:00",
				);
				$this->db->do_insert("tbl_assess", $info);
			}else{

				// 写入数据库
				$info = array(

				"assess_result"		  => $staff_assess,
				"create_time"         => date("Y-m-d H:i:s"),
				);
				$this->db->do_update("tbl_assess", $info, "staff_id='{$staff_id}' AND assess_year='{$time}'");
			}

			// 跳转
			Helper_Client::location("考核成功", "index.php?a=assess.assesslist");
		}

		$this->tpl->assign("staff_data", $staff_data);
		$this->tpl->assign("assess_data", $assess_data);
		$this->tpl->assign("assess_result", $assess_result);
		$this->tpl->display("assess/assess.update.tpl.php");
	}

}

?>